package cn.edu.nju.software.fantasyCard.screens;

import cn.edu.nju.software.fantasyCard.MainGame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;

/**
 *   创建者：            杨光 
 *   创建时间：        2014-2-22   下午5:04:17  
 */

public class GameScreen implements Screen {
	
	MainGame mainGame;
	Stage stage;
	
	public GameScreen(MainGame mainGame) {
		this.mainGame = mainGame;
	}

	@Override
	public void render(float delta) {
	    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	    stage.act(Gdx.graphics.getDeltaTime());
	    stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void show() {
		stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); 
		Gdx.input.setInputProcessor(stage);
		
		TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/kapai.pack"));
        float duration = 8f; 
        int maxwidth = Gdx.graphics.getWidth() ; 
        int maxheight = Gdx.graphics.getHeight();
 
        for (int i = 0; i < 3; i++) { 
            Image image = new Image(atlas.findRegion("kapai")); 
            image.setX(MathUtils.random(0, maxwidth)); 
            image.setY(MathUtils.random(0, Gdx.graphics.getHeight())); //随机出现 
            image.setScale(0.5f);
            Action moveAction = Actions.sequence(Actions.moveTo(MathUtils.random(0, 
                    maxwidth), MathUtils.random(0, maxheight), duration / 2), 
                    Actions.moveBy(MathUtils.random(0, maxwidth), MathUtils.random(0, 
                            maxheight), duration / 2)); //移动方向和地点随机 
            Action rotateAction = Actions.rotateTo(360, duration); //旋转 
            Action fadeAction = Actions.repeat(10, Actions.sequence(Actions.fadeOut(duration / 20), 
                    Actions.fadeIn(duration / 20))); //闪烁，重复10次 
            image.addAction(Actions.forever(Actions.parallel(moveAction, rotateAction, fadeAction))); //所有action并行 
            stage.addActor(image); 
        } 
		Table table = new Table();
		table.setFillParent(true);
		stage.addActor(table);		
		Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
		
		Label nameLabel = new Label("游戏", skin);
		TextureRegion upRegion = skin.getRegion("default-slider-knob");
		TextureRegion downRegion = skin.getRegion("default-slider-knob");
		BitmapFont buttonFont = skin.getFont("default-font");
		TextButton backButton = new TextButton("返回", skin);
		backButton.addListener(new InputListener() {
			@Override
			public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
				mainGame.setScreen(new MainMenuScreen(mainGame));
				return false;
			}
		});
		
		backButton.setX(200);
		backButton.setY(20);
		stage.addActor(backButton);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		
	}

	@Override
	public void dispose() {
		stage.dispose();
	}
}


